package com.export.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import common.util.CalculateUtil;
import lombok.Data;

import java.util.Objects;
import java.util.Optional;

/**
 * 计算率
 *
 * @author yuanqinglong
 * @since 2021/7/26 16:34
 */
@Data
public class CalculateRate {

    /**
     * 请求数
     */
    @ExcelProperty(value = "请求数")
    private Integer statReqNum;
    /**
     * 查询数
     */
    @ExcelProperty(value = "查询数")
    private Integer statQueryNum;
    /**
     * 查询率
     */
    @ExcelProperty(value = "查询率")
    private String statQueryRate;
    /**
     * 查得数
     */
    @ExcelProperty(value = "查得数")
    private Integer statFindoutNum;
    /**
     * 查得率
     */
    @ExcelProperty(value = "查得率")
    private String statFindoutRate;
    /**
     * 失败数
     */
    @ExcelProperty(value = "失败数")
    private Integer statFailNum;
    /**
     * 失败率
     */
    @ExcelProperty(value = "失败率")
    private String statFailRate;


    /**
     * 计算率
     */
    public void calculateRate() {
        if(statReqNum == 0){
            statFindoutRate = CalculateUtil.getPercent(0.00);
            statFailRate = CalculateUtil.getPercent(0.00);
        }else {
            statFindoutRate = Optional.ofNullable(statFindoutNum).map(integer -> CalculateUtil.getPercent(statFindoutNum / (double) statReqNum)).orElse(null);
            statFailRate = Optional.ofNullable(statFailNum).map(integer -> CalculateUtil.getPercent(statFailNum / (double) statReqNum)).orElse(null);
        }
        this.calculateQueryRate();
    }

    /**
     * 计算查询率
     */
    private void calculateQueryRate() {
        if (Objects.nonNull(statReqNum) && Objects.nonNull(statFailNum)) {
            if(statReqNum == 0){
                statQueryRate = CalculateUtil.getPercent(0.00);
            }else {
                statQueryNum = statReqNum - statFailNum;
                statQueryRate = CalculateUtil.getPercent(statQueryNum / (double) statReqNum);
            }
        }
    }

}
